package com.ellricky.baselib.util.photo;

import android.net.Uri;

import org.devio.takephoto.app.TakePhoto;
import org.devio.takephoto.compress.CompressConfig;
import org.devio.takephoto.model.CropOptions;
import org.devio.takephoto.model.LubanOptions;
import org.devio.takephoto.model.TakePhotoOptions;


public class TakePhotoUtil {


    public static void takeCamera(TakePhoto takePhoto, CropOptions cropOptions, Uri imageUri) {
        if (cropOptions == null) {
            takePhoto.onPickFromCapture(imageUri);
        } else {
            takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions);
        }
    }


    public static void takePicOne(TakePhoto takePhoto, CropOptions cropOptions, Uri imageUri) {
        if (cropOptions == null) {
            takePhoto.onPickFromGallery();
        } else {
            takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions);
        }
    }

    public static void takePicMulti(TakePhoto takePhoto, CropOptions cropOptions, int maxLimit) {
        if (maxLimit <= 1) {
            return;
        }
        if (cropOptions == null) {
            takePhoto.onPickMultiple(maxLimit);
        } else {
            takePhoto.onPickMultipleWithCrop(maxLimit, cropOptions);
        }
    }


    public static void configTakePhotoOption(TakePhoto takePhoto) {
        TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder()
                .setWithOwnGallery(true)
                .setCorrectImage(false);
        takePhoto.setTakePhotoOptions(builder.create());
    }


    public static void configCompress(TakePhoto takePhoto) {
        configCompress(takePhoto, 1024 * 1024, 800, 800, true, true);
    }

    public static void configCompress(TakePhoto takePhoto, int maxSize, int w, int h,
                                      boolean cacheRaw, boolean compressWithOwn) {
        CompressConfig config;
        if (compressWithOwn) {
            config = new CompressConfig.Builder()
                    .setMaxSize(maxSize)
                    .setMaxPixel(w >= h ? w : h)
                    .enablePixelCompress(false)
                    .enableReserveRaw(cacheRaw)
                    .create();
        } else {
            LubanOptions option = new LubanOptions.Builder()
                    .setMaxHeight(h)
                    .setMaxWidth(w)
                    .setMaxSize(maxSize)
                    .create();
            config = CompressConfig.ofLuban(option);
            config.enableReserveRaw(cacheRaw);
        }
        takePhoto.onEnableCompress(config, true);


    }

    public static CropOptions getCropOptions(int desW, int desH) {
        CropOptions.Builder builder = new CropOptions.Builder()
                .setAspectX(desW)
                .setAspectY(desH)
                .setWithOwnCrop(true);
        return builder.create();
    }


}
